[ARM] az deployment: Fix bicep template size inflation with differential template handling#31990
Conversation
- Bicep files now use JSON objects directly to prevent Azure SDK string escaping size inflation - ARM template files continue using string content with JsonCTemplatePolicy for JSONC compatibility - URI-based deployments use template_link without local processing - Conditional JsonCTemplatePolicy application only for ARM template files - Fixes false '4MB template too large' errors for bicep templates under the actual limit This implements a bicep-specific solution rather than modifying JsonCTemplatePolicy globally, ensuring ~20% size reduction for bicep templates while maintaining full ARM template compatibility.
- Extract _process_template_file helper function to handle bicep/ARM template processing - Reduces branches in _prepare_deployment_properties_unmodified - Maintains all existing functionality while improving code organization
- Extract _get_template_for_deployment helper function - Fix trailing whitespace issue - Reduces branch count in main function for pylint compliance
- Replace elif statements with separate if statements after return - Improves code clarity and follows pylint best practices
- Remove trailing whitespace from blank lines - Final style cleanup for CI compliance
️✔️AzureCLI-FullTest
|
|
Hi @vhvb1989, |
️✔️AzureCLI-BreakingChangeTest
|
|
Thank you for your contribution! We will review the pull request and get back to you soon. |
|
The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR. Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions). pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>
|
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a bicep template size inflation issue that was causing false "template too large" errors for deployments near the 4MB limit. The fix implements differential template processing based on file type to prevent unnecessary string escaping.
- Implements differential handling where bicep files use JSON objects directly while ARM templates continue using string content
- Removes JsonCTemplatePolicy application for bicep files to prevent ~20% size inflation from string escaping
- Refactors template preparation logic into reusable helper functions for better maintainability
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
…ts string content, not JSON objects
…e_file for consistency and DRY principle
The condition 'and not (template_file and is_bicep_file(template_file))' is no longer needed because: - JsonCTemplatePolicy only affects string content, not JSON objects - Bicep files use JSON objects (template_obj) directly - ARM templates use string content (template_content) where policy applies - Therefore, JsonCTemplatePolicy can always be applied safely
Clarifies that _deploy_arm_template_core_unmodified is resource-group-specific, so the hardcoded 'resourceGroup' scope is intentional and correct.
- Added TestTemplateSizeOptimization class with 8 focused unit tests - Tests validate differential handling between bicep and ARM templates - Bicep templates use template objects (size optimized) - ARM templates use template content (backward compatible) - URI deployments use template links (unchanged behavior) - All tests pass and existing functionality preserved - Validates size optimization benefits while maintaining compatibility
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
az deployment: Fix bicep template size inflation with differential template handling
|
@vhvb1989 Could you please recording some yaml files for the related tests in live mode? For how to record tests, please refer to this guideline https://github.com/Azure/azure-cli/blob/dev/doc/authoring_tests.md#recording-tests |
|
@vhvb1989 Please resolve these CI issues? |
|
/azp run |
|
Commenter does not have sufficient privileges for PR 31990 in repo Azure/azure-cli |
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
Description
Fixes bicep template size inflation that causes false "template too large" errors for deployments near the 4MB limit by implementing differential template processing based on file type.
Problem
The Azure CLI was applying JsonCTemplatePolicy string escaping to all template types, causing significant size inflation for bicep templates. This led to deployment failures for templates under 4MB due to the inflated request payload size.
Root Cause Analysis
Solution Approach
Implemented differential template handling based on file type:
Bicep Files (.bicep)
ARM Template Files (.json)
URI-based Deployments
Key Code Changes
_prepare_deployment_properties_unmodified(): Now uses differential handling where bicep files use JSON objects directly, ARM files use string content, and URI deployments use template links.
_deploy_arm_template_core_unmodified(): JsonCTemplatePolicy is now only applied to ARM template files, not bicep files, preventing unnecessary string escaping.
Impact
Testing
Related Issues
Fixes #31989
Type of Change
[✓] Bug fix (non-breaking change which fixes an issue)
[ ] New feature (non-breaking change which adds functionality)
[ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
[ ] This change requires a documentation update